import request from '../../utils/request'
Page({
        data: {
                phone: '123456',
                password: '111111',
        },
        bindKeyInputpassword: function (e) {
                this.setData({
                        password: e.detail.value
                })
        },
        bindKeyInputphone: function (e) {
                this.setData({
                        phone: e.detail.value
                })
        },

        formSubmit: function (e) {
                console.log('e->', this.data.phone);
                const phone = this.data.phone
                const code = this.data.password
                if (phone && code) {
                        // 这里需要添加登录验证的逻辑，例如调用后端接口
                        request({
                                url: "loginPhone",
                                method: 'GET',
                                data: {
                                        phone: phone,
                                        password: code
                                },
                                success(res) {
                                        if (res.data.code == 200) {
                                                console.log(res.data.msg);
                                                // 保存token
                                                wx.setStorageSync('token', res.data.token);
                                                // 登录成功后发送请求获取登录信息，同时为该登录对象授权
                                                request({
                                                        url: 'getPhoneInfo',
                                                        method: 'GET',
                                                        success(res) {
                                                                // 这里后期可以把当前登录的用户对象进行保存...
                                                                console.log(res.data.wxuser)
                                                                wx.setStorageSync('userid', res.data.wxuser.id)
                                                                wx.setStorageSync('avator', res.data.wxuser.avator)
                                                                wx.setStorageSync('name', res.data.wxuser.name)
                                                                wx.switchTab({
                                                                        url: '/pages/index/index',
                                                                })
                                                        }
                                                })
                                        }
                                }
                        });
                } else {
                        wx.showToast({
                                title: '请输入完整信息',
                                icon: 'none'
                        });
                }
        },
        onLoad() {
                if (wx.getStorageSync('token')) {
                        wx.switchTab({
                          url: '/pages/index/index',
                        })
                        
                }
                const eventChannel = this.getOpenerEventChannel()
                let t = this
                console.log('eventChannel->', eventChannel.on);
                if (eventChannel.on != null) {
                        // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
                        eventChannel.on('acceptDataFromOpenerPage', function (data) {
                                console.log('注册参数', data)
                                t.setData({
                                        phone: data.phone,
                                        password: data.password
                                })
                        })
                }


        }
});